﻿using System;
using System.Collections;
using System.Collections.Generic;
using OrderManage.DAL;
using OrderManage.Model;
using OrderManage.Utility;

namespace OrderManage.BLL
{
    public class OrderBLL
    {
        private readonly OrderDAL _dal = new OrderDAL();

        public string GetOrderList(OrderQcInfo qcmodel)
        {
            var total = _dal.GetTotal(qcmodel);
            var list = _dal.GetOrderList(qcmodel);
            return JsonHelper.Encode(total, list);
        }

        public bool AddOrder(string userName, string data)
        {
            var model = new OrderInfo();
            var rows = (ArrayList)JsonHelper.Decode(data);
            foreach (Hashtable row in rows)
            {
                model.UserName = userName;
                model.Mobile = row["Mobile"].ToString();
                model.RechargeAmount = Convert.ToInt32(row["RechargeAmount"].ToString());
            }
            return _dal.AddOrder(model);
        }
        public bool RemoveOrder(int id)
        {
            return _dal.RemoveOrder(id);
        }

        public bool ModifyErrorCodeAndText(int orderId, int status, int errorCode, string errorText)
        {
            return _dal.ModifyErrorCodeAndText(orderId, status, errorCode, errorText);
        }

        public List<OrderEntity> GetOrders(int status)
        {
            return _dal.GetOrders(status);
        }

        public List<OrderEntity> GetOrder(int status, int count)
        {
            return _dal.GetOrder(status, count);
        }

        public int GetOrderCount(int status)
        {
            return _dal.GetOrderCount(status);
        }

        public bool SaveOrder(OrderInfo model)
        {
            return _dal.AddOrder(model);
        }

        public string GetTotalAmount(OrderQcInfo qcmodel)
        {
            var model = new AmountInfo();
            var list = _dal.GetTotalAmount(qcmodel);
            foreach (var i in list)
            {
                switch (i.Status)
                {
                    //新订单
                    case 0:
                        model.TotalNewAmount = model.TotalNewAmount + i.RechargeAmount;
                        break;
                    //成功订单
                    case 1:
                        model.TotalSuccessAmount = model.TotalSuccessAmount + i.RechargeAmount;
                        break;
                    //失败订单
                    case 2:
                        model.TotalFailureAmount = model.TotalFailureAmount + i.RechargeAmount;
                        break;
                }
            }
            return JsonHelper.Encode(model);
        }

        public string GetTotalRechargeAmountChartList(string tyear)
        {
            return JsonHelper.Encode(_dal.GetTotalRechargeAmountChartList(tyear));
        }
    }
}
